<?php
class Product
{
    private $products;
    
    ################################################
    #   PUBLIC METHODS
    ################################################
    
    /**
     * __construct
     *
     * initializes the Product object.
     *
     * @author Scotty Delicious
     */
    public function __construct()
    {
        global $modx;
        $this->products = $modx->getFullTableName('mxw_products');
    }
    
    /**
     * find
     *
     * returns a product object or an array of 
     * product objects depending on whether the
     * $id parameter was a product_id or 'all'.
     *
     * @param string $id 
     * @return object $product || array $product_array
     * @author Scotty Delicious
     */
    public function find( $id = 'all' )
    {
        global $modx;
        
        if ($id == 'all' || $id == '*')
        {
            return $this->_allProducts();
        }
        
        else
        {
            return $this->_product( $id );
        }
    }
    
    /**
     * save
     *
     * @return void
     * @author Scotty Delicious
     */
    public function save()
    {
        # [TODO] Will save the product to the DB.
    }
    
    ################################################
    #   PRIVATE METHODS
    ################################################
    
    /**
     * _allProducts
     *
     * Returns an array of product objects 
     * with a basic set of attributes.
     *
     * @return array $product_array
     * @author Scotty Delicious
     */
    private function _allProducts()
    {
        global $modx;
        $product_array = array();
        
        $sql = "SELECT * FROM ".$this->products." WHERE `parent_id` IS NULL";
        $allProducts = $modx->db->query( $sql );
        
        while ($row = $modx->db->getRow( $allProducts ))
        {
            $p = new stdClass;
            $p->id = $row['product_id'];
            $p->name =$row['product'];
            $p->sku = $row['sku'];
            $p->price = $row['price'];
            $p->inventory = $row['inventory'];
            $p->description = $row['description'];
            $p->created_on = $row['created_on'];
            $p->modified_on = $row['modified_on'];

            $sql = "SELECT `product_id` FROM ".$this->products." WHERE `parent_id` = ".$p->id;
            $v = $modx->db->query( $sql );

            $p->variations = $modx->db->getRecordCount( $v );
            $product_array[] = $p;
            unset($p);
        }
        
        return $product_array;
    }
    
    /**
     * _product
     *
     * Returns a the product object requested
     * by $_POST['product_id']
     *
     * @param string $product_id 
     * @return object $product
     * @author Scotty Delicious
     */
    private function _product( $product_id )
    {
        global $modx;
        $product = new stdClass;
        
        $sql = "SELECT * FROM ".$this->products." WHERE `product_id` = ".$product_id." OR `parent_id` = ".$product_id;
        $pQuery = $modx->db->query( $sql );
        
        while ($row = $modx->db->getRow( $pQuery ))
        {
            if ($row['product_id'] == $product_id)
            {
                if ($product->variations) # [TODO] I don't know if this check is needed?
                {
                    $pv = $product->variations;
                    $product = $this->_assembleProductForEditing( $row );
                    $product->variations = $pv;
                }
                
                else
                {
                    $product = $this->_assembleProductForEditing( $row );
                }
            }
            
            else
            {
                $product->variations[] = $this->_assembleProductForEditing( $row );
            }
        }
        return $product;
    }
    
    
    private function _assembleProductForEditing( $row )
    {
        $p = new stdClass;
        $p->id = $row['product_id'];
        $p->name =$row['product'];
        $p->sku = $row['sku'];
        $p->price = $row['price'];
        $p->inventory = $row['inventory'];
        $p->description = $row['description'];
        $p->created_on = $row['created_on'];
        $p->modified_on = $row['modified_on'];
        return $p;
    }
    
    /**
     * _productExists
     *
     * @return boolean
     * @author Scotty Delicious
     */
    private function _productExists()
    {
        # [TODO] This will help determine UPDATE vs INSERT
    }
    
    /**
     * _hasOption
     *
     * @param string $optionName 
     * @return boolean
     * @author Scotty Delicious
     */
    private function _hasOption($optionName)
    {
        # [TODO] This will return which option sets the product
        #        has access to.
    }
    
}